package eu.tolu.cableSpotter;

import java.io.IOException;
import java.util.ArrayList;

import eu.tolu.cableSpotter.help.AppPreferences;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddCable extends Activity {
	
	private String cableName = "";
	private ArrayList<String> cableEdit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_cable);
		
		
		final Bundle extras = getIntent().getExtras();
		if (extras != null) {
			cableName = extras.getString("cableName");
			if(!cableName.equals("")){
				cableEdit = new AppPreferences(this).getCustomeCableByName(cableName);
				
				((EditText) findViewById(R.id.et_addcable_country)).setText(cableEdit.get(0));
				((EditText) findViewById(R.id.et_addcable_name)).setText(cableEdit.get(1));
				((EditText) findViewById(R.id.et_addcable_add1)).setText(cableEdit.get(2));
				((EditText) findViewById(R.id.et_addcable_add2)).setText(cableEdit.get(3));
				((EditText) findViewById(R.id.et_addcable_add3)).setText(cableEdit.get(4));
				((EditText) findViewById(R.id.et_addcable_phone)).setText(cableEdit.get(5));
				((EditText) findViewById(R.id.et_addcable_fax)).setText(cableEdit.get(6));
				((EditText) findViewById(R.id.et_addcable_email)).setText(cableEdit.get(7));
				((EditText) findViewById(R.id.et_addcable_web)).setText(cableEdit.get(8));
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_cable, menu);
		return true;
	}

    public void storeCableButton(final View view) {
    	String cableName = ((EditText) findViewById(R.id.et_addcable_name)).getText().toString();
    	String cableCountry = ((EditText) findViewById(R.id.et_addcable_country)).getText().toString();
    	String cableAdd1 = ((EditText) findViewById(R.id.et_addcable_add1)).getText().toString();
    	String cableAdd2 = ((EditText) findViewById(R.id.et_addcable_add2)).getText().toString();
    	String cableAdd3 = ((EditText) findViewById(R.id.et_addcable_add3)).getText().toString();
    	String cablePhone = ((EditText) findViewById(R.id.et_addcable_phone)).getText().toString();
    	String cableFax = ((EditText) findViewById(R.id.et_addcable_fax)).getText().toString();
    	String cableEmail = ((EditText) findViewById(R.id.et_addcable_email)).getText().toString();
    	String cableWeb = ((EditText) findViewById(R.id.et_addcable_web)).getText().toString();
    	
    	ArrayList<String> cableData = new ArrayList<String>();
    	
		cableData.add(0, cableCountry);// 0
		cableData.add(1, cableName);// 1
		cableData.add(2, cableAdd1);// 2
		cableData.add(3, cableAdd2);// 3
		cableData.add(4, cableAdd3);// 4
		cableData.add(5, "");// 5
		cableData.add(6, cablePhone);// 6
		cableData.add(7, cableFax);// 7
		cableData.add(8, cableEmail);// 8
		cableData.add(9, cableWeb);// 9
		cableData.add(10, "");// 10
		cableData.add(11, "custome");// 11
		cableData.add(12, "");// 11
		
		AppPreferences appPref = new AppPreferences(this);
		try {
			appPref.setCustomeCableArray(cableData);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Toast.makeText(getApplicationContext(), "Cable added", Toast.LENGTH_SHORT).show();
		startActivity(new Intent(this, MainActivity.class));
		finish();
    }
}
